package com.briup.jd2511.jd2511_product_source.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.briup.jd2511.jd2511_product_source.dao.ManagerAnimalMapper;
import com.briup.jd2511.jd2511_product_source.dao.ManagerBatchMapper;
import com.briup.jd2511.jd2511_product_source.dao.ManagerHurdlesMapper;
import com.briup.jd2511.jd2511_product_source.dao.ext.ManagerAnimalExtMapper;
import com.briup.jd2511.jd2511_product_source.exception.ServiceException;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerAnimal;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerBatch;
import com.briup.jd2511.jd2511_product_source.pojo.ManagerHurdles;
import com.briup.jd2511.jd2511_product_source.pojo.ext.ManagerAnimalExt;
import com.briup.jd2511.jd2511_product_source.result.ResultCode;
import com.briup.jd2511.jd2511_product_source.service.ManagerAnimalService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class ManagerAnimalServiceImpl implements ManagerAnimalService {

    @Autowired
    private ManagerAnimalExtMapper managerAnimalExtMapper;
    @Autowired
    private ManagerAnimalMapper managerAnimalMapper;
    @Autowired
    private ManagerBatchMapper managerBatchMapper;
    @Autowired
    private ManagerHurdlesMapper managerHurdlesMapper;

    @Override
    public PageInfo<ManagerAnimalExt> getAllByConditionByPage(String aHealthy, String aGender, Integer pageNum, Integer pageSize) throws ServiceException {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo<>(managerAnimalExtMapper.selectAllByCondition(aHealthy,aGender));
    }

    @Override
    public void saveOrUpdate(ManagerAnimal managerAnimal) throws ServiceException {
        // 参数校验
        String animalId = managerAnimal.getAAnimalId();
        String healthyStatus = managerAnimal.getAHealthy();

        // 验证健康状态是否为空
        if (StrUtil.isBlank(healthyStatus)) {
            throw new ServiceException(ResultCode.PARAM_IS_EMPTY);
        }

        if (StrUtil.isBlank(animalId)) {
            // 新增动物信息
            managerAnimal.setAAnimalId(IdUtil.fastSimpleUUID());

            // 获取批次信息并设置动物状态
            String batchId = managerAnimal.getABatchId();
            ManagerBatch batch = managerBatchMapper.selectById(batchId);
            if (Objects.isNull(batch)) {
                throw new ServiceException(ResultCode.DATA_IS_EMPTY);
            }
            String quarantineStatus = batch.getBQuarantine();
            managerAnimal.setAStatus("已检疫".equals(quarantineStatus) ? "已检疫" : "养殖中");

            // 插入新动物记录
            managerAnimalMapper.insert(managerAnimal);

            // 更新栏圈已存动物数及满载状态
            String hurdlesId = managerAnimal.getAHurdlesId();
            ManagerHurdles hurdles = managerHurdlesMapper.selectById(hurdlesId);
            Integer currentSaved = hurdles.getHSaved() + 1;
            Integer maxCapacity = hurdles.getHMax();

            if (currentSaved.equals(maxCapacity)) {
                hurdles.setHSaved(currentSaved);
                hurdles.setHFull("已满");
                managerHurdlesMapper.updateSelective(hurdles);
            } else {
                hurdles.setHSaved(currentSaved);
                managerHurdlesMapper.updateSelective(hurdles);
            }

        } else {
            // 修改动物信息
            ManagerAnimal existingAnimal = managerAnimalMapper.selectById(animalId);
            if (Objects.isNull(existingAnimal)) {
                throw new ServiceException(ResultCode.DATA_IS_EMPTY);
            }

            // 如果更换了栏圈，则更新原栏圈与新栏圈的保存数量和状态
            String newHurdlesId = managerAnimal.getAHurdlesId();
            String oldHurdlesId = existingAnimal.getAHurdlesId();

            if (!oldHurdlesId.equals(newHurdlesId)) {
                ManagerHurdles newHurdles = managerHurdlesMapper.selectById(newHurdlesId);
                ManagerHurdles oldHurdles = managerHurdlesMapper.selectById(oldHurdlesId);

                // 更新新栏圈状态
                int newSavedCount = newHurdles.getHSaved() + 1;
                newHurdles.setHSaved(newSavedCount);
                newHurdles.setHFull(newSavedCount == newHurdles.getHMax() ? "已满" : "未满");
                managerHurdlesMapper.updateSelective(newHurdles);

                // 更新旧栏圈状态
                int oldSavedCount = oldHurdles.getHSaved() - 1;
                oldHurdles.setHSaved(oldSavedCount);
                oldHurdles.setHFull("已满".equals(oldHurdles.getHFull()) ? "未满" : oldHurdles.getHFull());
                managerHurdlesMapper.updateSelective(oldHurdles);
            }

            // 执行更新操作
            managerAnimalMapper.updateSelective(managerAnimal);
        }
    }
}
